/**
 * 新闻管理
 */
<template>
  <div>
    <!-- 表格展示所有新闻 -->
    <el-table :data="newsList" border>
      <el-table-column prop="new_id" label="新闻ID"></el-table-column>
      <el-table-column prop="new_title" label="新闻标题"></el-table-column>
      <el-table-column prop="new_column" label="新闻栏目"></el-table-column>
      <el-table-column prop="new_content" label="新闻内容"></el-table-column>
      <el-table-column prop="new_time" label="创建时间"></el-table-column>
      <el-table-column prop="new_state" label="审核状态"></el-table-column>
      <el-table-column label="操作" width="300">
        <template slot-scope="scope">
          <!-- 审核通过按钮 -->
          <el-button v-if="scope.row.new_state === 0" size="small" type="primary" @click="approveNews(scope.row.new_id)">
            审核通过
          </el-button>
          <!-- 审核不通过按钮 -->
          <el-button v-if="scope.row.new_state === 0" size="small" type="danger" @click="rejectNews(scope.row.new_id)">
            审核不通过
          </el-button>
          <!-- 删除按钮 -->
          <el-button type="danger"size="small"  @click="deleteNews(scope.row.new_id)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { getAllNews, getNewsByState, approveNews,rejectNews,deleteNews } from '../../api/basisMG';
import Pagination from '../../components/Pagination';
export default {
  data() {
    return {
      newsList: [], // 存放新闻数据的数组
    };
  },
  created() {
    // 获取所有新闻数据
    this.getAllNews();
  },
  methods: {
    // 获取所有新闻
    getAllNews() {
      getAllNews()
        .then((response) => {
          // 将获取到的新闻数据存储到 newsList
          this.newsList = response.data;
        })
        .catch((error) => {
          console.error('获取新闻数据失败：', error);
        });
    },
    // 审核通过新闻
    approveNews(newsId) {
      approveNews(newsId)
        .then(() => {
          // 审核通过后刷新新闻列表
          this.getAllNews();
        })
        .catch((error) => {
          console.error('审核通过新闻失败：', error);
        });
    },
    // 审核不通过新闻
    rejectNews(newsId) {
      rejectNews(newsId)
        .then(() => {
          // 审核不通过后刷新新闻列表
          this.getAllNews();
        })
        .catch((error) => {
          console.error('审核不通过新闻失败：', error);
        });
    },
    // 删除新闻
    deleteNews(newsId) {
      deleteNews(newsId)
        .then(() => {
          // 删除后刷新新闻列表
          this.getAllNews();
        })
        .catch((error) => {
          console.error('删除新闻失败：', error);
        });
    },
  },
};
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
</style>




